/**
 * Created by asus on 2017/4/28.
 */
import http from '../../../common/httpUtil'
import * as types from './mutationTypes'

const actions = {
  querySysLessons({commit}, {title = '', index = 1, size = 10} = {}) {
    commit(types.LESSON_QUERY_LESSONS_BEGIN)
    let url = `sys/course/list?title=${title}&page=${index}&size=${size}`;
    return http.getRaw(url).then(result => {
      let lessons = result.body.objs
      let currentPage = result.body.page
      let totalCount = result.body.totalNum
      commit(types.LESSON_QUERY_LESSONS_SUC, {lessons, currentPage, totalCount})
    })
  },
  deleteLessons({commit}, params) {
    /*
    let data = {
      courseId: params.courseId
    };
    */
    let url = `sys/course/delete/${params.courseId}`;
    return http.delete(url).then(() => {
      commit(types.LESSON_DELETE_SUC)
    })
  },
  batchDeleteLessons({commit}, params) {
    let ids = [];
    for (let k = 0; k < params.length; k++) {
      ids.push(params[k].courseId);
    }
    let query = ids.join(',');
    let url = `sys/course/batch-delete/${query}`;
    return http.delete(url).then(() => {
      commit(types.LESSON_DELETE_SUC)
    })
  },
  setEditingLesson({commit}, lesson) {
    //{}
    commit(types.LESSON_SET_EDITING_LESSON, {lesson})
  },
  setPageModel({commit}, modelType) {
    //''
    commit(types.LESSON_SET_PAGE_MODEL, {modelType})
  },
  setLessonSearchText({commit}, searchText) {
    commit(types.LESSON_SET_SEARCH_TEXT, {searchText})
  },
  searchLessons({commit}, searchText) {
    commit(types.LESSON_SEARCH_LESSONS, {searchText})
  },
  releaseLesson({commit}, lessonData) {
    let url = 'sys/course/add-or-update';
    return http.post(url, lessonData).then(data => {
      commit(types.LESSON_RELEASE_SUC)
    })
  },
  updateLesson({commit}, lessonData) {
    let url = 'sys/course/add-or-update';
    return http.post(url, lessonData).then(data => {
      commit(types.LESSON_UPDATE_SUC)
    })
  }
}
export default actions
